package Function;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import daoimpl.MySQLProduktBatchDAO;
import daoimpl.MySQLRaavareBatchDAO;
import daoimpl.MySQLRaavareDAO;
import daoimpl.MySQLReceptDAO;
import daoimpl.MySQLOperatoerDAO;
import daointerfaces.DALException;
import dto.ProduktBatchDTO;
import dto.RaavareBatchDTO;
import dto.RaavareDTO;
import dto.ReceptDTO;
import dto.OperatoerDTO;

public class Functions {

	public MySQLReceptDAO receptDAO;
	public MySQLOperatoerDAO operatorDAO;
	public MySQLProduktBatchDAO produktbatchDAO;
	public MySQLRaavareBatchDAO raavarebatchDAO;
	public MySQLRaavareDAO raavareDAO;

	public Functions(){
		receptDAO = new daoimpl.MySQLReceptDAO();
		operatorDAO = new daoimpl.MySQLOperatoerDAO();
		produktbatchDAO = new daoimpl.MySQLProduktBatchDAO();
		raavareDAO = new MySQLRaavareDAO();
		raavarebatchDAO = new MySQLRaavareBatchDAO();
		try {
			new connector.Connector();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public String logIn(int ID, String password) throws DALException{
		try {
			OperatoerDTO temp = operatorDAO.getOperatoer(ID);
			if(temp.getPassword().equals(password))
				return temp.getRights();
		} catch (DALException e) {
			throw new DALException("Invalid Login");
		}
		return null;
	}

	public List<ReceptDTO> getReceptList(){
		try {
			return receptDAO.getReceptList();
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public List<ReceptDTO> getReceptListNr(int receptNr){
		try {
			return receptDAO.getReceptListNr(receptNr);
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public List<ReceptDTO> getReceptListGroup(){
		try {
			return receptDAO.getReceptListGroup();
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}


	public void createRecept(int receptID, String receptNavn, String[][] Raavare){	//String[][] Raavare	
		for (String[] strings : Raavare) {
			int raavareID = Integer.parseInt(strings[0]);
			double nomNetto = Double.parseDouble(strings[1]);
			double tolerance = Double.parseDouble(strings[2]);

			try {
				receptDAO.createRecept(new ReceptDTO(receptID, receptNavn, raavareID, nomNetto, tolerance));
			} catch (DALException e) {
				System.out.println(e.getMessage());
			}
		}
	}

	public void deleteRecept(int receptId){
		try {
			receptDAO.deleteRecept(receptId);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public void updateRecept(ReceptDTO id){
		try {
			receptDAO.updateRecept(id);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public OperatoerDTO getOperatoer(int ID){
		try {
			return operatorDAO.getOperatoer(ID);
		} catch (DALException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void createOperatoer(OperatoerDTO opr){
		try {
			operatorDAO.createOperatoer(opr);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public void deleteOperatoer(int oprId){
		try {
			operatorDAO.deleteOperatoer(oprId);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public List<OperatoerDTO> getOperatoerListGroup(){
		try {
			return operatorDAO.getOperatoerListGroup();
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public List<OperatoerDTO> getOperatoerListNr(int oprId){
		try {
			return operatorDAO.getOperatoerListNr(oprId);
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public void updateOperaoer(OperatoerDTO opr){
		try {
			operatorDAO.updateOperatoer(opr);
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
	}

	public String getRecept(int receptId) throws DALException {
		String recept = receptDAO.getRecept(receptId).toString();
		return recept;
	}

	public List<ProduktBatchDTO> getProduktBatchList(){
		try {
			return produktbatchDAO.getProduktBatchList();
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public List<ProduktBatchDTO> getProduktBatchListNr(int pbId){
		try {
			return produktbatchDAO.getProduktBatchGroup(pbId);
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public List<ProduktBatchDTO> getProduktBatchListGroup(){
		try {
			return produktbatchDAO.getProduktBatchGroupBypbId();
		} catch (DALException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	public void createProduktBatch(ProduktBatchDTO pbId) throws DALException{
		try {
			produktbatchDAO.createProduktBatch(pbId);
		} catch (DALException e) {
			throw new DALException("Invalid input");
		}
	}

	public void deleteProduktBatch(int pbId){
		try {
			produktbatchDAO.deleteProduktBatch(pbId);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public void updateProduktBatch(ProduktBatchDTO batch){
		try {
			produktbatchDAO.updateProduktBatch(batch);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public List<RaavareBatchDTO> getRaavareBatchList(){
		try {
			return raavarebatchDAO.getRaavareBatchList();
		} catch (DALException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void createRaavareBatch(RaavareBatchDTO id){
		try {
			raavarebatchDAO.createRaavareBatch(id);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public void deleteRaavareBatch(int rbId){
		try {
			raavarebatchDAO.deleteRaavareBatch(rbId);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public void updataRaavareBatch(RaavareBatchDTO id){
		try {
			raavarebatchDAO.updateRaavareBatch(id);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}

	public List<RaavareDTO> getRaavareList(){
		try {
			return raavareDAO.getRaavareList();
		} catch (DALException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void updateRaavare(RaavareDTO id){
		try{
			raavareDAO.updateRaavare(id);
		}catch(Exception e){
			System.out.println(e.getMessage() + " Dovenskab");
		}
	}

	public void createRaavare(RaavareDTO id){
		try {
			raavareDAO.createRaavare(id);
		} catch (Exception e) {
			System.out.println(e.getMessage() + " Dovenskab");
		}
	}

	public String generateINI(String oprNavn){
		String ini = "";
		try{
		String[] tempIni = oprNavn.split(" ");
		for (String string : tempIni) {
			ini = ini + string.substring(0, 1);
		}
		}catch(Exception e){
			System.out.println("Could not generate ini");
		}
		return ini;
	}

	public String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH.mm.ss");
		Calendar cal = Calendar.getInstance();
		String output = sdf.format(cal.getTime());
		return output;
	}

	public void deleteRaavare(int raavareId){
		try {
			raavareDAO.deleteRaavare(raavareId);
		} catch (DALException e) {
			e.printStackTrace();
		}
	}
	
	public boolean checkInt(String text) {
		try {
			Integer.parseInt(text);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

}
